<?php
/**
 * 补零操作
 * @param $num1
 * @param $num2
 * @return string
 */
function addStrings($num1, $num2) {
    $len1 = strlen($num1);
    $len2 = strlen($num2);
    $num  = $len1 - $len2;
    $char = '';
    $status = $num>0 ? 1 : 0;
    $num = $num > 0 ? $num : -$num;
    for ($i = 0; $i < $num; $i++) {
        $char .= "0";
    }
    if (!$status){
        $num1 = $char.$num1;
        $len = $len2;
    }else{
        $num2 = $char.$num2;
        $len = $len1;
    }
    $num3 = "";
    $arr = 0;
    for ($j = $len-1; $j >= 0; $j--) {
        $value = $num1[$j] + $num2[$j] + $arr;
        $arr = 0;
        if ($value >= 10) {
            $value = $value - 10;
            $arr = 1;
        }
        $num3[$j] = $value;
    }
    if ($arr == 1){
        $num3 = '1'.$num3;
    }
    return $num3;
}

function addStrings2($num1,$num2)
{
    $len1 = strlen($num1) -1;
    $len2 = strlen($num2) -1;
    $arr = 0;//进位标识
    $num3 = '';
    while ($len1 >= 0 || $len2 >= 0 || $arr) { //三个条件任意满足一个
        $sum = $arr;
        $sum += $len1>=0 ? $num1[$len1] : 0;
        $sum += $len2 >=0 ? $num2[$len2] : 0;
        $arr = 0;
        if ($sum >=10){
            $sum = $sum - 10;
            $arr = 1;
        }
        $num3.=$sum;
        $len1--;
        $len2--;
    }
    return strrev($num3);
}

$res = addStrings2("77",'23');
echo $res;